﻿Imports Microsoft.VisualBasic

Namespace SimilText.Tools

    Public Class Dictionary
        Inherits Dictionary(Of String, UInteger)

        Public Event OnChange(ByVal n As Integer)

        Public Words() As DictionaryElement
        Public DB As mDB = mDB.GetDB

        Public StemmingFile As String
        Public Sub New()

        End Sub

        'Public Sub CreateDictionary()
        '    'Dim MaxId As Integer = -1
        '    Dim ref() As UInteger
        '    Dim KeyRefIds As String


        '    DB.ExecuteCommand("truncate table dictionary")

        '    Dim q_dic = DB.TabDictionary_Link.ToList

        '    For Each d In q_dic
        '        Me(d.Word) = d.Word_Id
        '    Next

        '    'Dim ins As New FastSqlInsert(DB, "derivate_dictionary")
        '    DB.InitMultiRowsInsert()

        '    ' clica tutte le parole
        '    For Each w In Me
        '        ref = New UInteger() {}

        '        q_dic = DB.TabDictionary_Link.Where("Word", "=", w.Key).ToList
        '        ' cli tutte la parole uguali trovate nel dizionario
        '        For Each d In q_dic
        '            'mette i figli
        '            Dim q_dic_f = DB.TabDictionary_Link.Where("Word_Ref_Id", "=", d.Word_Id).ToList
        '            For Each d_f In q_dic_f
        '                Me.AddWordRef(ref, w.Value, Me(d_f.Word))
        '            Next

        '            'se ha un riferimento aggiunge altre cose
        '            If d.Word_Ref_Id <> 0 Then
        '                'mette il padre
        '                Dim d_p = DB.TabDictionary_Link.Where("Word_Id", "=", d.Word_Ref_Id).Limit(1).ToObj '(From dic In DB.dictionaries Where dic.WordId = d.WordRefId).First
        '                Me.AddWordRef(ref, w.Value, Me(d_p.Word))

        '                'mette i fratelli
        '                Dim q_dic_b = DB.TabDictionary_Link.Where("Word_Ref_Id", "=", d.Word_Ref_Id).ToList
        '                For Each d_b In q_dic_b
        '                    Me.AddWordRef(ref, w.Value, Me(d_b.Word))
        '                Next
        '            End If

        '        Next
        '        'scrivi i dati su db
        '        KeyRefIds = ""
        '        For ct_k As Integer = 0 To ref.Length - 1
        '            KeyRefIds &= ref(ct_k).ToString & ";"
        '        Next
        '        KeyRefIds = KeyRefIds.Trim(New Char() {";"})

        '        DB.InsertRowOnSubmit(New mDB.Dictionary With {.Word_Id = w.Value, .Word = w.Key, .Word_Ref_Ids = KeyRefIds})

        '        'DB.SubmitChanges()
        '        'DB.InitMultiRowsInsert()

        '        If w.Value Mod 1000 = 0 Then
        '            RaiseEvent OnChange(w.Value)
        '            System.Diagnostics.Debug.Print(w.Value.ToString)
        '        End If

        '    Next

        '    DB.SubmitChanges()

        'End Sub

        Public Sub StemmingTextToBin()

        End Sub

        Public Sub StemmingDBToText()
            'Dim MaxId As Integer = -1
            Dim ref As New Dictionary(Of String, Integer)


            'DB.ExecuteCommand("truncate table dictionary")

            Dim frw = New IO.StreamWriter(StemmingFile)

            Dim TabDic = DB.TAB.Dictionary_Link_It

            Dim q_dic = TabDic.ToList

            For Each d In q_dic
                Me(d.Word) = d.Word_Id
            Next

            'Dim ins As New FastSqlInsert(DB, "derivate_dictionary")
            'DB.InitMultiRowsInsert()


            ' clica tutte le parole
            For Each w In Me
                ref.Clear()

                q_dic = TabDic.Where("Word", "=", w.Key).ToList
                ' cli tutte la parole uguali trovate nel dizionario
                For Each d In q_dic
                    'mette i figli
                    Dim q_dic_f = TabDic.Where("Word_Ref_Id", "=", d.Word_Id).ToList
                    For Each d_f In q_dic_f
                        Me.AddWord(ref, w.Key, d_f.Word)
                    Next

                    'se ha un riferimento aggiunge altre cose
                    If d.Word_Ref_Id <> 0 Then
                        'mette il padre
                        Dim d_p = TabDic.Where("Word_Id", "=", d.Word_Ref_Id).Limit(1).ToObj '(From dic In DB.dictionaries Where dic.WordId = d.WordRefId).First
                        Me.AddWord(ref, w.Key, d_p.Word)

                        'mette i fratelli
                        Dim q_dic_b = TabDic.Where("Word_Ref_Id", "=", d.Word_Ref_Id).ToList
                        For Each d_b In q_dic_b
                            Me.AddWord(ref, w.Key, d_b.Word)
                        Next
                    End If

                Next
                'scrivi i dati su db
                If ref.Count > 0 Then
                    Dim row = w.Key

                    For Each r In ref
                        row &= "," & r.Key
                    Next
                    frw.WriteLine(row)
                End If

                If w.Value Mod 1000 = 0 Then
                    RaiseEvent OnChange(w.Value)
                    System.Diagnostics.Debug.Print(w.Value.ToString)
                End If

            Next

            frw.Close()

            ' DB.SubmitChanges()

        End Sub

        Public Sub AddWord(ByRef ref As Dictionary(Of String, Integer), ByVal Word As String, ByVal WordRef As String)

            If Word = WordRef Then Exit Sub

            If Not ref.ContainsKey(WordRef) Then
                ref.Add(WordRef, 1)
            End If

        End Sub
        'Public Sub AddWordRef(ByRef ref() As UInteger, ByVal WordId As UInteger, ByVal WordRefId As UInteger)

        '    If WordId = WordRefId Then Exit Sub

        '    If Array.IndexOf(ref, WordRefId) = -1 Then
        '        ReDim Preserve ref(ref.Length)
        '        ref(ref.Length - 1) = WordRefId
        '    End If


        'End Sub


    End Class


End Namespace
